package controller;
import java.util.ArrayList;
import java.util.List;

import service.SysFuncService;






import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.test.util.JsonResult;

import entity.SysFunc;
import entity.SysRoleFunc;
import service.SysRoleFuncService;
/**
 * Created by wenchao.ren on 2014/4/26.
 */
@SpringBootApplication
//启注解事务管理  
@Controller
@Import ({SysRoleFuncService.class,SysFuncService.class})
public class SysRoleFuncController {

	@Autowired
	private SysRoleFuncService sysRoleFuncService;
	@Autowired
	private SysFuncService sysFuncService;

	@RequestMapping(value = "SysRoleFuncList", method = RequestMethod.GET)
    public String SysRoleFuncList(Model model) {
    	//ModelAndView view = new ModelAndView("/sys/SysRoleFuncList");
    	List<SysRoleFunc> SysRoleFuncList = sysRoleFuncService.find();
    	//view.addObject("SysRoleFuncList", SysRoleFuncList);
    	model.addAttribute("SysRoleFuncList", SysRoleFuncList);
        return "sys/sysRoleFuncList";
    }
	
	
	@ResponseBody
	@RequestMapping(value = "SysRoleFunc_list.json")
	public List<SysRoleFunc> listSysRoleFunc() {
		List<SysRoleFunc> userList = sysRoleFuncService.find();
		return userList;
	}
	
	@RequestMapping(value = "SysRoleFuncEdit", method = RequestMethod.GET)
    public String userEdit(Long  roleId,Model model) {

		//获取所有func
		List<SysFunc> sysFuncList = sysFuncService.findAllPreFunc();
	
		List<SysFunc> sysFuncPreList = new ArrayList<>();
		
				for(SysFunc sysFunc:sysFuncList){
					List<SysFunc> sysFuncSub2List = new ArrayList<>();
					List<SysFunc> sysFuncSubList = sysFuncService.findByPreId(sysFunc.getSeqid());
					System.out.println("sysFuncSubList.size"+sysFuncSubList.size());
					for(SysFunc sysFuncSub:sysFuncSubList){
						if(sysRoleFuncService.findByRoleIdAndFuncId(roleId,sysFuncSub.getSeqid())!=null){
							sysFuncSub.setChecked(1);
						}else{
							sysFuncSub.setChecked(0);
						}
						sysFuncSub2List.add(sysFuncSub);
					}
					if(sysRoleFuncService.findByRoleIdAndFuncId(roleId,sysFunc.getSeqid())!=null){
						sysFunc.setChecked(1);
					}else{
						sysFunc.setChecked(0);
					}
					System.out.println("sysFuncSub2List.size"+sysFuncSub2List.size());
					sysFunc.setSysFuncSubList(sysFuncSub2List);
					sysFuncPreList.add(sysFunc);
				}

		
    	
    	
    	model.addAttribute("sysFuncPreList", sysFuncPreList);
    	model.addAttribute("roleId", roleId);
    	 return "sys/sysRoleFuncEdit";
    }
	
	/**
	 * 保存用户修改信息
	 * 
	 * @param tblStage
	 * @return
	 */
	@RequestMapping(value = "sysRoleFunc_save.json")
	@ResponseBody
	public JsonResult saveUser(SysRoleFunc sysRoleFunc,long roleId) {
		JsonResult result = JsonResult.newInstance();
		sysRoleFuncService.saveByRoleIdAndFuncStr(sysRoleFunc,roleId);
		return result;
	}

	/**
	 * 批量删除用户
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "SysRoleFunc_del.json")
	@ResponseBody
	public JsonResult delUsers(String ids) {
		JsonResult result = JsonResult.newInstance();
		sysRoleFuncService.deleteByIds(ids.split(","));
		return result;
	}
	
	
    
}
